//index.js
import Model from './model'
let model = new Model()
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    height: 0,
    a: '',
    scroll: 0,
    offset:0,
    limit: 3,  //一页多少条数据
    pagenum: 1, //当前页码
    pagesize: 0, //总页数
    items: [],
    cateltems: [],
    soldGood: [],
    indexDataBlock: [],
    product: []   //产品数据
  },

  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  demo1:function(e) {
    console.log(e);
  },
  onLoad: function () {
    // 首页数据
    var that = this;
    model.getlist({
      offset: this.data.offset,
      limit: this.data.limit
    }).then((ret) => {
      console.log(ret);
      //计算总页数
      const total = Math.ceil(ret.data.sum/this.data.limit)
      console.log(total);
      
      this.setData({
        items: ret.data.data,
        offset: this.data.limit + this.data.items.length,
        pagesize: total
      })
      
    })
    
    //获取首页分类数据
    model.getcate().then((ret) => {
      wx.hideLoading()
      this.setData({
        cateltems: ret.data.data
      })
      console.log(this.data.cateltems);
      
    })
    //获取畅销好课数据
    model.getSoldGood().then((ret) => {
      this.setData({
        soldGood: ret.data.data
      })
    })
    //首页数据块
    model.getIndexDataBlock().then((ret) => {
      this.setData({
        indexDataBlock: ret.data.data
      })
    })
    // this.getmore()
    //屏幕高度
    var that = this
    wx.getSystemInfo({
      success (res) {
        // console.log(res.windowHeight)
        that.setData({
          height:res.windowHeight
        })
      }
    })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  top(e) {
      var scrollTop = e.detail.scrollTop
      if(scrollTop >= 200) {
        this.setData({
           a: 'topshow'
        })
      }else {
        this.setData({
          a: ''
        })
      }
  },
  scrollTop() {
    this.setData({
      scroll: 0
    })
  },
  // 页面触底事件
  onReachBottom() {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    console.log('触底');
    this.setData({
      limit: this.data.limit+1,
      pagenum: this.data.pagenum+1
    })
    model.getlist({
      offset: 0,
      limit: this.data.limit
    }).then((ret) => {
      if(this.data.pagenum >= this.data.pagesize+1) {
        wx.showLoading({
          title: '没有更多数据了',
          mask: true
        })
      }else {
        console.log('下');
        
      }
      setTimeout(function () {
        wx.hideLoading()
      }, 1000)
      
      this.setData({
        items: ret.data.data,
        offset: this.data.limit + this.data.items.length
      })


      console.log(this.data.items.length);
      
    })


  }
})
